package com.kly.api.imagesearch.sdk;

import com.microsoft.azure.cognitiveservices.search.imagesearch.BingImageSearchAPI;
import com.microsoft.azure.cognitiveservices.search.imagesearch.BingImageSearchManager;
import com.microsoft.azure.cognitiveservices.search.imagesearch.models.ImageObject;
import com.microsoft.azure.cognitiveservices.search.imagesearch.models.ImagesModel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author admin
 * @date 2025/3/14
 */
@Slf4j
public class BingSearchImage {


    public static ImageObject searchImage(String imageUrl) {

        final String subscriptionKey = "COPY_YOUR_KEY_HERE";
        String searchTerm = "canadian rockies";
// Image search client
        BingImageSearchAPI client = BingImageSearchManager.authenticate(subscriptionKey);

        ImagesModel imageResults = client.bingImages().search()
                .withQuery(searchTerm)
                .withMarket("en-us")
                .execute();

        if (imageResults != null && imageResults.value().size() > 0) {
            // Image results
            ImageObject firstImageResult = imageResults.value().get(0);

            return firstImageResult;
        } else {
            System.out.println("Couldn't find image results!");
        }
        return null;
    }
}
